package com.iweb.service;

import com.common.datasource.DataSourceUtil;
import com.common.model.DataGrid;
import com.common.model.Page;
import com.iweb.dao.OrderDao;
import com.iweb.dao.OrderDaoImpl;
import com.iweb.pojo.Order;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class OrderService {
    public DataGrid<Order> findOrderList(Page page) {
        DataGrid<Order> dataGridord = new DataGrid<>();
        // 链接数据库
        DataSourceUtil dataSourceUtil = new DataSourceUtil();
        dataSourceUtil.init();
        // 创建数据库操作对象
        OrderDao orderDao = new OrderDaoImpl(dataSourceUtil.getConnection());

        try {

            long count =orderDao.selectCount();
            List<Order> list = orderDao.findAllOrder(page);
            dataGridord.setCode(0);
            dataGridord.setMsg("");
            dataGridord.setCount(count);
            dataGridord.setData(list);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            dataSourceUtil.close();
        }
        return dataGridord;
    }

    public DataGrid<Order> findOrderNopay(Page page) {
        DataGrid<Order> dataGridord = new DataGrid<>();
        // 链接数据库
        DataSourceUtil dataSourceUtil = new DataSourceUtil();
        dataSourceUtil.init();
        // 创建数据库操作对象
        OrderDao orderDao = new OrderDaoImpl(dataSourceUtil.getConnection());

        try {

            long count =orderDao.selectCountNopay();
            List<Order> list = orderDao.findNopayOrder(page);
            dataGridord.setCode(0);
            dataGridord.setMsg("");
            dataGridord.setCount(count);
            dataGridord.setData(list);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            dataSourceUtil.close();
        }
        return dataGridord;
    }

    public DataGrid<Order> findOrderPayed(Page page) {
        DataGrid<Order> dataGridord = new DataGrid<>();
        // 链接数据库
        DataSourceUtil dataSourceUtil = new DataSourceUtil();
        dataSourceUtil.init();
        // 创建数据库操作对象
        OrderDao orderDao = new OrderDaoImpl(dataSourceUtil.getConnection());

        try {

            long count =orderDao.selectCountPayed();
            List<Order> list = orderDao.findPayedOrder(page);
            dataGridord.setCode(0);
            dataGridord.setMsg("");
            dataGridord.setCount(count);
            dataGridord.setData(list);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            dataSourceUtil.close();
        }
        return dataGridord;
    }
    public synchronized boolean addOrder(Order orders) {

        // 链接数据库
        DataSourceUtil dataSourceUtil = new DataSourceUtil();
        dataSourceUtil.init();
        // 创建数据库操作对象
        OrderDao orderDao = new OrderDaoImpl(dataSourceUtil.getConnection());

        try {
            Order order = orderDao.findById(orders.getOrder_id());
            // 验证用户是否可以注册
            if(order != null){
                throw new RuntimeException();
            }
            // 注册用户
            // 设定主键
            orders.setOrder_id(UUID.randomUUID().toString());
            //设定日期
            SimpleDateFormat df = new SimpleDateFormat("MM-dd HH:mm:ss");//设置日期格式
            orders.setOrder_date(df.format(new Date()));
            orders.setOrder_sta("待付款");
            orderDao.save(orders);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }finally {
            dataSourceUtil.close();
        }
    }

    public void apply(String order_id) {
        // 链接数据库
        DataSourceUtil dataSourceUtil = new DataSourceUtil();
        dataSourceUtil.init();
        // 创建数据库操作对象
        OrderDao orderDao = new OrderDaoImpl(dataSourceUtil.getConnection());

        try {
            orderDao.updateById(order_id);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            dataSourceUtil.close();
        }
    }
}
